import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadDemo30PC {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue=new LinkedBlockingQueue<>();
        Thread producer=new Thread(()->{
            int n=0;
            while (true){
                try {
                    queue.put(n);//offer不会阻塞，put才行
                    System.out.println("生产元素"+n);
                    n++;
                    Thread.sleep(2000);
                }catch(InterruptedException e){
                    throw new RuntimeException(e);
                }
            }
        },"producer");

        Thread consumer=new Thread(()->{
            while(true){
                try{
                    Integer n=queue.take();
                    System.out.println("消费元素"+n);

                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    throw new RuntimeException(e);
                }
            }
        },"consumer");
        producer.start();
        consumer.start();
    }
}
